# -*- conding:UTF-8 -*-

'''
@ Project: day1_zy
@ File: code.py
@ Author: 罗婷
@ Date：2022/5/18 19:13

'''
# 导入pymysql flask 导入配置文件
import pymysql
from flask import Flask
from settings import DefaultConfig

# 实例化对象
app = Flask(__name__)
# 从对象加载配置
app.config.from_object(DefaultConfig)


@app.route('/')

def index():
    mysql_host = app.config.get('MYSQL_HOST')
    mysql_port = app.config.get('MYSQL_PORT')
    print('从对象加载配置的调用')
    return 'day01_hw'



class Student(object):
        # 连接mysql
        def __init__(self):
            self.con = pymysql.connect(host='localhost', port=3306, user='root', password='password', database='sjk518', charset='utf8')
            self.p1 = self.con.cursor()  # cursor()获取游标对象

        # 创表
        def create_tb(self):
            try:
                sql = """create table students(
                id int,
                name varchar(10),
                age int,
                sex varchar(2)
                )  """
                self.p1.execute(sql)

            except Exception as e:
                print(e)
            else:
                print('创表成功')

        # 增加学生
        def add_stu(self):
            sql = """insert into students values(1, '张三', 19, '男'),
              (2, '李四', 14, '女'),
              (3, '王五', 25, '女');"""
            ret = self.p1.execute(sql)
            if ret:
                self.con.commit()
                print('添加学生成功')
            else:
                self.con.rollback()
                print('添加失败')

        # 删除学生
        def delete(self):
            sql = """delete from students where id=3"""
            ret = self.p1.execute(sql)
            if ret:
                self.con.commit()
                print('删除学生成功')
            else:
                self.con.rollback()
                print('删除失败')

        # 修改学生
        def update(self):
            sql = """update students set age=99 where id=2"""
            ret = self.p1.execute(sql)
            if ret:
                self.con.commit()
                print('修改学生成功')
            else:
                self.con.rollback()
                print('修改失败')

        # 查询所有学生
        def show(self):
            sql = """select * from students"""
            self.p1.execute(sql)
            print(self.p1.fetchall())

stu = Student()

# 调用创表函数
# stu.create_tb()
# 增加学生
# stu.add_stu()
# 删除学生
# stu.delete()
# 修改
# stu.update()
# 查询
stu.show()

if __name__ == '__main__':
    app.run()








##############
#         def insert(self):
#             sql = """insert into stu values(1,'张三',23,'男'),(2,'李四',22,'女'),(3,'王五',1,'女');"""
#             ret = self.p1.execute(sql)
#             # 游侠对象执行spl语句
#             if ret:
#                 self.con.commit()
#                 print("插入成功")
#             else:
#                 self.con.rollback()
#                 print("插入失败")
#
#         def update(self):
#             sql = """update stu set gender = '女' where id = 2;"""
#             ret = self.p1.execute(sql)
#             if ret:
#                 self.con.commit()
#                 print("修改成功")
#             else:
#                 self.con.rollback()
#                 print("修改失败")
#
#         def delete(self):
#             sql = """delete from stu where id = 2;"""
#             ret = self.p1.execute(sql)
#             if ret:
#                 self.con.commit()
#                 print("删除成功")
#             else:
#                 self.con.rollback()
#                 print("删除失败")
#
#         def select(self):
#             sql = """select * from stu;"""
#             self.p1.execute(sql)
#             print(self.p1.fetchall())
#
#     mysql = Student()
#
#     # 用哪个解开哪个注释
#     # mysql.create()
#     mysql.insert()
#     # mysql.update()
#     # mysql.delete()
#     # mysql.select()
#
#     return "hello_flask"
#
#
# if __name__ == '__main__':
#     app.run()












